package com.lky.marshall.search.thread;

import java.util.concurrent.*;

/**
 * Created by
 * autor : Marshall lky
 * datetime: 2021/4/20 19:47
 */
public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);

//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        System.out.println("main ... start ...");
////        1. 继承Thread;
////        Thread01 thread01 = new Thread01();
////        thread01.start();  //启动线程
////        2. 实现Runnable接口；
////        Runnable01 runnable01 = new Runnable01();
////        new Thread(runnable01).start();
////        3. 实现Callable接口+FutureTask
////        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
////        new Thread(integerFutureTask).start();
////        Integer integer = integerFutureTask.get(); //等待线程执行完成获取返回结果
////        4. 线程池
//        //给线程池直接提交任务
//        //保证当前系统中池只有一两个,每个异步任务,直接提交给线程池自己执行
//        service.execute(new Runnable01());
//
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
//                100,
//                20,
//                TimeUnit.SECONDS,
//                new LinkedBlockingDeque<>(11111),    //new LinkedBlockingDeque<>() 默认的Integer的最大值,如果不自定义最大数量,会资源耗尽
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());
//
//        System.out.println("main ... end ...");
//    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main ... start ...");
//        CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
//            System.out.println("当前线程:"+Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果:"+i);
//        }, service);

        /**
         * 方法成功完成后的处理
         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, service).whenComplete((res,excption)->{
//            System.out.println(";异步任务成功完成了...结果是:"+res+"异常是:"+excption);
//        }).exceptionally(excption->{
//            return 10;
//        });

        /**
         * 方法失败完成后的处理
         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 3;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, service).handle((res,exception)->{
//            if (res!=null){
//                return res*2;
//            }
//            if (exception!=null) {
//                return 0;
//            }
//            return 0;
//        });

        /**
         * 串行化：
         *  thenRun:不能获取上一步的执行结果
         *  thenRunAsync(()->{
         *             System.out.println("任务2启动...");
         *         },service)
         *
         *  thenAccept:能获取上一步的结果，但是没有返回值
         *  thenAcceptAsync((res)->{
         *             System.out.println("线程B获取的结果："+res);
         *         },service)
         *  thenApply:能获取上一步结果，有返回值
         *  thenApplyAsync(res -> {
         *             System.out.println("线程B获取的结果："+res);
         *             return res;
         *         })
         */
//        CompletableFuture<Integer> intFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, service).thenApplyAsync(res -> {
//            System.out.println("线程B获取的结果："+res);
//            return res;
//        });
        /**
         * 两个都完成
         */
//        CompletableFuture<Object> intFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("intFuture启动:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("intFuture结束:" + i);
//            return i;
//        }, service);
//
//        CompletableFuture<Object> stringFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("stringFuture开始:");
////            try {
////                Thread.sleep(2000);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//            System.out.println("stringFuture结束:");
//            return "hello";
//        }, service);

//        intFuture.runAfterBothAsync(stringFuture,()->{
//            System.out.println("任务3开始:");
//            System.out.println("任务3结束:");
//        },service);

//        CompletableFuture<Void> voidCompletableFuture = intFuture.thenAcceptBothAsync(stringFuture, (intFutureRes, stringFutureRes) -> {
//            System.out.println("任务3开始:");
//            System.out.println("任务3结束:");
//            System.out.println("intFutureRes:"+intFutureRes);
//            System.out.println("stringFutureRes:"+stringFutureRes);
//        }, service);
        //R apply(T t, U u);
//        CompletableFuture<String> future = intFuture.thenCombineAsync(stringFuture, (intFutureRes, stringFutureRes) -> {
//            return stringFutureRes+intFutureRes;
//        }, service);
//

        /**
         * 两个任务，只要有一个完成，就执行任务3
         */
//        intFuture.runAfterEitherAsync(stringFuture,()->{
//            System.out.println("任务3开始之前的结果是：");
//        },service);

//        stringFuture.acceptEitherAsync(intFuture,(res)->{
//            System.out.println("任务3开始之前的结果是：");
//            System.out.println(res);
//        },service);
//        CompletableFuture<Object> future = stringFuture.applyToEitherAsync(intFuture, res -> {
//            System.out.println("任务3开始之前的结果是：" + res);
//            return res;
//        }, service);

        CompletableFuture<String> jpgFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("商品的图片信息");
            return "tupian.jpg";
        }, service);

        CompletableFuture<String> attrFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("商品的属性信息");
            return "黑色+256G";
        }, service);

        CompletableFuture<String> decsFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("商品的介绍");
            return "Huawei";
        }, service);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(jpgFuture, attrFuture, decsFuture);
//        allOf.get();//等待所有结果完成

        CompletableFuture<Object> future = CompletableFuture.anyOf(jpgFuture, attrFuture, decsFuture);

        System.out.println("main ... end ..."+future.get());
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }


}
